﻿<%@ Page Title="Hợp đồng" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="QuanLyHopDong.Web.HopDong.List" %>

<%@ Import Namespace="QuanLyHopDong.Web.Common" %>
<%@ Import Namespace="Resources" %>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <div class="filtering">
        <table>
            <tr>
                <td>Tên hợp đồng:</td>
                <td>
                    <input type="text" id="tenHopDong" class="name" /></td>
            </tr>
            <tr>
                <td>Đơn vị:</td>
                <td>
                    <input type="text" id="tenDonVi" class="name" /></td>
            </tr>
            <tr>
                <td>Số hợp đồng:</td>
                <td>
                    <input type="text" id="soHopDong" class="code" /></td>
            </tr>
            <tr>
                <td>Ngày quyết toán:</td>
                <td>
                    <input type="text" id="ngayQuyetToanFrom" class="date" />
                    -
                    <input type="text" id="ngayQuyetToanTo" class="date" /></td>
            </tr>
            <tr>
                <td>Tình trạng hợp đồng:</td>
                <td>
                    <select id="ddlTrangThai">
                        <option value="0"></option>
                        <option value="1">Chưa ký hợp đồng</option>
                        <option value="2">Đã ký, đang thi công</option>
                        <option value="3">Đang hoàn công</option>
                        <option value="4">Đang quyết toán</option>
                        <option value="5">Hoàn thiện</option>
                    </select>
                </td>
            </tr>
            <tr id="ngayHoanThienRow" style="display: none;">
                <td>Ngày hoàn thiện:</td>
                <td>
                    <input type="text" id="ngayHoanThienFrom" class="date" />
                    -
                    <input type="text" id="ngayHoanThienTo" class="date" /></td>
            </tr>
            <tr>
                <td>Nhân viên:</td>
                <td>
                    <asp:DropDownList ID="ddlEmployees" runat="server"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <label>
                        <input type="checkbox" id="chkThiCongXong" />Thi công xong, chờ hoàn công</label></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <button type="submit" id="LoadRecordsButton"><%=GlobalStrings.LoadRecords %></button>
                    <button type="submit" id="ResetButton"><%=GlobalStrings.Reset %></button></td>
            </tr>
        </table>

    </div>
    <p>
        <asp:Button ID="btnNew2" runat="server" Text="<%$ Resources:GlobalStrings, AddNew %>" OnClientClick="javascript: window.location = 'New.aspx'; return false;" />
        <input type="button" value="Xuất Excel" onclick="XuatExcel()">
    </p>
    <div id="ListContainer">
    </div>
    <p>
        <input type="button" value="Xuất Excel" onclick="XuatExcel()">
        <asp:Button ID="btnNew" runat="server" Text="<%$ Resources:GlobalStrings, AddNew %>" OnClientClick="javascript: window.location = 'New.aspx'; return false;" />
    </p>

    <script type="text/javascript">
        function XuatExcel() {
            window.location = 'HopDongExport.aspx?' +
                'donVi=' + $('#tenDonVi').val() +
                '&soHopDong=' + $('#soHopDong').val() +
                '&tenHopDong=' + $('#tenHopDong').val() +
                '&ngayQuyetToanFrom=' + $('#ngayQuyetToanFrom').val() +
                '&ngayQuyetToanTo=' + $('#ngayQuyetToanTo').val() +
                '&trangThaiHopDongId=' + $('#ddlTrangThai').val() +
                '&ngayHoanThienFrom=' + $('#ngayHoanThienFrom').val() +
                '&ngayHoanThienTo=' + $('#ngayHoanThienTo').val() +
                '&employeeId=' + $('#<%=ddlEmployees.ClientID%>').val() +
                '&thiCongXong=' + $('#chkThiCongXong').is(':checked');
        }

        $(function () {
            $('#ngayHoanThienFrom').calendar({ timeSeparators: null });
            $('#ngayHoanThienTo').calendar({ timeSeparators: null });
            $('#ngayQuyetToanFrom').calendar({ timeSeparators: null });
            $('#ngayQuyetToanTo').calendar({ timeSeparators: null });

            $("#tenDonVi").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "/DanhMuc/DonVi/Default.aspx/SearchDonVis",
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify({
                            term: extractLast(request.term)
                        }),
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.value,
                                    value: item.value
                                };
                            }));
                        }
                    });
                }
            });

            $('#ListContainer').jtable({
                title: 'Hợp đồng',
                paging: true,
                pageSize: 10,
                sorting: true,
                defaultSorting: 'ThiCongXong DESC',
                actions: {
                    listAction: 'List.aspx/HopDongList'
                    <% if (WebCommon.CurrrentEmployee.IsAdmin)
                       { %>
                    , deleteAction: 'List.aspx/HopDongDelete'
                    <% }%>
                },
                fields: {
                    ThiCongXong: {
                        title: '',
                        width: '1%',
                        display: function (data) {
                            if (data.record.SanSangHoanCong == true)
                                return '<img src="/Content/red.png" />';
                            else
                                return '<img src="/Content/green.png" />';
                        }
                    },
                    Edit: {
                        title: '',
                        width: '1%',
                        display: function (data) {
                            return '<input title="<%=GlobalStrings.Edit %>" class="jtable-command-button jtable-edit-command-button" onclick="javascript:window.location=\'Edit.aspx?id=' + data.record.Id + '\';" />';
                        },
                        sorting: false
                    },
                    Id: {
                        title: "ID",
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    TenHopDong: {
                        title: 'Tên hợp đồng',
                        display: function(data) {
                            return '<a target="_blank" title="Chi tiết hợp đồng" href="HopDongChiTiet.aspx?hopDongId=' + data.record.Id + '">' + data.record.TenHopDong + '</a>';
                        }
                    },
                    TenDonVi: {
                        title: 'Đơn vị'
                    },
                    SoHopDong: {
                        title: 'Số hợp đồng'
                    },
                    NgayKy: {
                        title: 'Ngày ký', type: 'date',
                        displayFormat: 'dd/mm/yy'
                    },
                    NgayHoanThien: {
                        title: 'Ngày hoàn thiện', type: 'date',
                        displayFormat: 'dd/mm/yy'
                    },
                    NgayQuyetToan: {
                        title: 'Ngày quyết toán', type: 'date',
                        displayFormat: 'dd/mm/yy'
                    },
                    TinhTrangHopDong: { title: 'Tình trạng HĐ' }
                }
            });

            //Re-load records when user click 'load records' button.
            $('#LoadRecordsButton').click(function (e) {
                e.preventDefault();
                $('#ListContainer').jtable('load', {
                    soHopDong: $('#soHopDong').val(),
                    tenHopDong: $('#tenHopDong').val(),
                    tenDonVi: $('#tenDonVi').val(),
                    ngayHoanThienFrom: $('#ngayHoanThienFrom').val(),
                    ngayHoanThienTo: $('#ngayHoanThienTo').val(),
                    ngayQuyetToanFrom: $('#ngayQuyetToanFrom').val(),
                    ngayQuyetToanTo: $('#ngayQuyetToanTo').val(),
                    trangThai: $('#ddlTrangThai').val(),
                    thiCongXong: $('#chkThiCongXong').is(':checked'),
                    employeeId: $('#<%=ddlEmployees.ClientID%>').val()
                });
            });

            // Reset button
            $('#ResetButton').click(function (e) {
                e.preventDefault();
                $('#soHopDong').val('');
                $('#tenHopDong').val('');
                $('#tenDonVi').val('');
                $('#ngayHoanThienFrom').val('');
                $('#ngayHoanThienTo').val('');
                $('#ngayQuyetToanFrom').val('');
                $('#ngayQuyetToanTo').val('');
                $('#ddlTrangThai').val('0');
                $('#chkThiCongXong').prop('checked', false);
                <% if (WebCommon.CurrrentEmployee.IsAdmin)
                   { %>
                $('#<%= ddlEmployees.ClientID %>').val('0');
                <% } %>
                $('#ngayHoanThienRow').hide();
                $('#LoadRecordsButton').click();
            });

            // hoan thien
            $('#ddlTrangThai').change(function () {
                if ($(this).val() == 5) {
                    $('#ngayHoanThienRow').show();
                } else {
                    $('#ngayHoanThienRow').hide();
                }
            });

            if ($('#ddlTrangThai').val() == 5) {
                $('#ngayHoanThienRow').show();
            }

            //Load all records when page is first shown
            $('#LoadRecordsButton').click();
        });

        function split(val) {
            return val.split(/,\s*/);
        }

        function extractLast(term) {
            return split(term).pop();
        }
    </script>
</asp:Content>
